<?php
/**
* Class Export into Bo Controller
* @author SillySmart
* @copyright SillySmart
* @package Mvc.Controllers.Bo
* @see Mvc.Controllers.Bo.ControllerProtected
* @see Mvc.Controllers.SiteProtected
* @see Sls.Controllers.Core.SLS_GenericController
* @since 1.0
*
*/
class BoExport extends BoControllerProtected
{
	public function init()
	{
		parent::init();
	}

	public function action()
	{
		$http = $this->_generic->getObjectHttpRequest();
		$columns = array();
		$clause = array();
		$join = array();
		// Model + Db alias + Join
		$model = $http->getParam("Export_Model");
		$db = $http->getParam("Export_Db");
		$nJoin = $http->getParam("Export_Join");
		if (!empty($nJoin))
			$join = explode(",",$nJoin);
		$nbColumns = count(SLS_Sql::getInstance()->getColumnsName($model));
		// Columns
		foreach($http->getParams() as $key => $value)
			if (SLS_String::startsWith($key,"Export_Column_"))
				array_push($columns,$value);
		// Pagination + Order
		$column = ($http->getParam("column") == "|sls_empty|") ? "" : $http->getParam("column");
		$order = ($http->getParam("order") == "|sls_empty|") ? "" : $http->getParam("order");
		// Filters
		for($i=0 ; $i<$nbColumns ; $i++)
		{
			if ($http->getParam("filter_exact_".($i+1)) == "null" ||
				$http->getParam("filter_exact_".($i+1)) == "notnull" ||
				$http->getParam("filter_value_".($i+1)) != "")
			{
				if ($http->getParam("filter_value_".($i+1)) != "|sls_empty|")
				{
					$arrayTmp = array("column" => $http->getParam("filter_name_".($i+1)),
									"value"  => ($http->getParam("filter_exact_".($i+1)) == "in") ? explode(",",$http->getParam("filter_value_".($i+1))) : $http->getParam("filter_value_".($i+1)),
									"mode"   => $http->getParam("filter_exact_".($i+1)));
									array_push($clause,$arrayTmp);
				}
			}
		}
		if ($this->_generic->useModel($model,$db))
		{
			if (!empty($column) && !empty($order))
				$order = array(0=>array("column"=>$column,"order"=>$order));
			else
				$order = array();
			$className = ucfirst($db)."_".SLS_String::tableToClass($model);
			$object = new $className();
			$entities = $object->searchModels("",$join,$clause,array(),$order);
			$type = $http->getParam("Export_Type");
			$xmlType = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/types.xml"));
			$xmlFilter = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/filters.xml"));
			$types = array();
			switch($type)
			{
				case "excel":
					$title = $object->getTableComment($model,$db)." Listing";
					header("Content-Type: application/vnd.ms-excel");
					header("Content-Disposition: attachment; filename=\"".$title."\".xls");
					$src = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style="font-family:\'Franklin Gothic Book\', Verdana, Helvetica, Arial;"><table>';
					$src .= '<tr>';
					$tables = array($object->getTable());
					foreach($join as $cur_join)
						array_push($tables,(is_array($cur_join)) ? $cur_join["table"] : $cur_join);
					for($k=0 ; $k<$count3=count($columns) ; $k++)
					{
						$description = $object->getColumnComment($columns[$k],$model,$db);
						if ($description == $columns[$k] && !empty($join))
							$description = $object->getColumnComment($columns[$k],$join[0],$db);
						$src .= '<td style="text-align:center;background-color:#CAC3ED;color:#100F16;border:1px solid gray;font-weight:bold;font-size:14px;">'.$description.'</td>';

						foreach($tables as $table)
						{
							// Search possible types
							$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$table."' and @column='".$columns[$k]."' and (@type = 'file_img' or @type = 'file_all' or @type = 'email' or @type = 'url')]","type"));
							if (!empty($result["attribute"]))
							{
								$types[$k] = $result["attribute"];
								break;
							}
							else
								$types[$k] = "";

							// Search possible filters
							$result = array_shift($xmlFilter->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$table."' and @column='".$columns[$k]."' and @filter='hash']","hash"));
							if (!empty($result["attribute"]))
							{
								$types[$k] = "password";
								break;
							}
						}
					}
					$src .= '</tr>';
					for($i=0 ; $i<$count=count($entities) ; $i++)
					{
						$src .= '<tr>';
						for($j=0 ; $j<$count2 = count($columns) ; $j++)
						{
							switch ($types[$j])
							{
								case "password":
									$entities[$i]->$columns[$j] = "********";
									break;
								case "url":
									$entities[$i]->$columns[$j] = (SLS_String::isValidUrl($entities[$i]->$columns[$j])) ? "<a href='".$entities[$i]->$columns[$j]."' target='_blank'>".$entities[$i]->$columns[$j]."</a>" : "";
									break;
								case "file_all":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "<a href='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' target='_blank'>".$entities[$i]->$columns[$j]."</a>" : "";
									break;
								case "file_img":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "<a href='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' target='_blank'><img src='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' alt='".$entities[$i]->$columns[$j]."' width='80' style='max-width:80px;' /></a>" : "";
									break;
								case "email":
									$entities[$i]->$columns[$j] = "<a href='mailto:".$entities[$i]->$columns[$j]."' target='_blank'>".$entities[$i]->$columns[$j]."</a>";
									break;
							}
							$src .= '<td style="border:1px solid gray">'.(($entities[$i]->$columns[$j] == "") ? "&nbsp;" : $entities[$i]->$columns[$j]).'</td>';
						}
						$src .= '</tr>';
					}
					$src .= '</table></body></html>';
					echo $src;
					die();
					break;
				case "html":
					$title = $object->getTableComment($model,$db)." Listing";
					header("Content-Type: text/html");
					header("Content-Disposition: attachment; filename=\"".$title."\".html");
					$src = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style="font-family:\'Franklin Gothic Book\', Verdana, Helvetica, Arial;"><table cellpadding="5" cellspacing="0">';
					$src .= '<tr>';
					$tables = array($object->getTable());
					foreach($join as $cur_join)
						array_push($tables,(is_array($cur_join)) ? $cur_join["table"] : $cur_join);
					for($k=0 ; $k<$count3=count($columns) ; $k++)
					{
						$description = $object->getColumnComment($columns[$k],$model,$db);
						if ($description == $columns[$k] && !empty($join))
							$description = $object->getColumnComment($columns[$k],$join[0],$db);
						$src .= '<td style="text-align:center;background-color:#CAC3ED;color:#100F16;border:1px solid gray;font-weight:bold;font-size:14px;">'.$description.'</td>';

						foreach($tables as $table)
						{
							// Search possible types
							$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$table."' and @column='".$columns[$k]."' and (@type = 'file_img' or @type = 'file_all' or @type = 'email' or @type = 'url')]","type"));
							if (!empty($result["attribute"]))
							{
								$types[$k] = $result["attribute"];
								break;
							}
							else
								$types[$k] = "";

							// Search possible filters
							$result = array_shift($xmlFilter->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$table."' and @column='".$columns[$k]."' and @filter='hash']","hash"));
							if (!empty($result["attribute"]))
							{
								$types[$k] = "password";
								break;
							}
						}
					}
					$src .= '</tr>';
					for($i=0 ; $i<$count=count($entities) ; $i++)
					{
						$src .= '<tr>';
						for($j=0 ; $j<$count2 = count($columns) ; $j++)
						{
							switch ($types[$j])
							{
								case "password":
									$entities[$i]->$columns[$j] = "********";
									break;
								case "url":
									$entities[$i]->$columns[$j] = (SLS_String::isValidUrl($entities[$i]->$columns[$j])) ? "<a href='".$entities[$i]->$columns[$j]."' target='_blank'>".$entities[$i]->$columns[$j]."</a>" : "";
									break;
								case "file_all":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "<a href='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' target='_blank'>".$entities[$i]->$columns[$j]."</a>" : "";
									break;
								case "file_img":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "<a href='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' target='_blank'><img src='http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]."?".uniqid()."' alt='".$entities[$i]->$columns[$j]."' style='max-width:80px;' /></a>" : "";
									break;
								case "email":
									$entities[$i]->$columns[$j] = "<a href='mailto:".$entities[$i]->$columns[$j]."' target='_blank'>".$entities[$i]->$columns[$j]."</a>";
									break;
							}
							$src .= '<td style="border:1px solid gray">'.(($entities[$i]->$columns[$j] == "") ? "&nbsp;" : $entities[$i]->$columns[$j]).'</td>';
						}
						$src .= '</tr>';
					}
					$src .= '</table></body></html>';
					echo $src;
					die();
					break;
				case "csv":
					$title = $object->getTableComment($model,$db)." Listing";
					header("Content-Type: text/csv");
					header("Content-Disposition: attachment; filename=\"".$title."\".csv");
					$src = '';
					for($k=0 ; $k<$count3=count($columns) ; $k++)
					{
						$description = $object->getColumnComment($columns[$k],$model,$db);
						if ($description == $columns[$k] && !empty($join))
							$description = $object->getColumnComment($columns[$k],$join[0],$db);
						$src .= str_replace(",","\,",$description);
						if ($k < ($count3-1))
							$src .= ';';

						// Search possible types
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$object->getTable()."' and @column='".$columns[$k]."' and (@type = 'file_img' or @type = 'file_all' or @type = 'url')]","type"));
						$types[$k] = (!empty($result["attribute"])) ? $result["attribute"] : "";

						// Search possible filters
						$result = array_shift($xmlFilter->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$table."' and @column='".$columns[$k]."' and @filter='hash']","hash"));
						$types[$k] = (!empty($result["attribute"])) ? "password" : "";
					}
					$src .= "\n";
					for($i=0 ; $i<$count=count($entities) ; $i++)
					{
						for($j=0 ; $j<$count2=count($columns) ; $j++)
						{
							switch ($types[$j])
							{
								case "password":
									$entities[$i]->$columns[$j] = "********";
									break;
								case "url":
									$entities[$i]->$columns[$j] = (SLS_String::isValidUrl($entities[$i]->$columns[$j])) ? $entities[$i]->$columns[$j] : "";
									break;
								case "file_all":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j] : "";
									break;
								case "file_img":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j] : "";
									break;
							}
							$src .= '"'.str_replace(array('"',"\n","\t","\r"),array('""','','',''),$entities[$i]->$columns[$j]).'"';
							if ($j < ($count2-1))
								$src .= ';';
						}
						$src .= "\n";
					}
					echo $src;
					die();
					break;
				case "txt":
					$title = $object->getTableComment($model,$db)." Listing";
					header("Content-Type: text/tab-separated-value");
					header("Content-Disposition: attachment; filename=\"".$title."\".tsv");
					$src = '';
					for($k=0 ; $k<$count3=count($columns) ; $k++)
					{
						$description = $object->getColumnComment($columns[$k],$model,$db);
						if ($description == $columns[$k] && !empty($join))
							$description = $object->getColumnComment($columns[$k],$join[0],$db);
						$src .= $description;
						if ($k < ($count3-1))
							$src .= "\t";

						// Search possible types
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$object->getDatabase()."_".$object->getTable()."' and @column='".$columns[$k]."' and (@type = 'file_img' or @type = 'file_all' or @type = 'password' or @type = 'url')]","type"));
						$types[$k] = (!empty($result["attribute"])) ? $result["attribute"] : "";
					}
					$src .= "\n";
					for($i=0 ; $i<$count=count($entities) ; $i++)
					{
						for($j=0 ; $j<$count2=count($columns) ; $j++)
						{
							switch ($types[$j])
							{
								case "password":
									$entities[$i]->$columns[$j] = "********";
									break;
								case "url":
									$entities[$i]->$columns[$j] = (SLS_String::isValidUrl($entities[$i]->$columns[$j])) ? $entities[$i]->$columns[$j] : "";
									break;
								case "file_all":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j] : "";
									break;
								case "file_img":
									$entities[$i]->$columns[$j] = (!is_dir($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j]) && file_exists($this->_generic->getPathConfig("files").$entities[$i]->$columns[$j])) ? "http://".$this->_generic->getSiteConfig("domainName")."/".$this->_generic->getPathConfig("files").$entities[$i]->$columns[$j] : "";
									break;
							}
							$src .= $entities[$i]->$columns[$j];
							if ($j < ($count2-1))
								$src .= "\t";
						}
						$src .= "\n";
					}
					echo $src;
					die();
					break;
			}
		}
	}
}
?>